package cn.jksoft.businessclient.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.yokeyword.indexablelistview.IndexEntity;
import com.yokeyword.indexablelistview.IndexableStickyListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jksoft.businessclient.R;
import cn.jksoft.businessclient.api.ReceivingService;
import cn.jksoft.businessclient.base.BaseApplication;
import cn.jksoft.businessclient.base.BaseSwipeBackCompatActivity;
import cn.jksoft.businessclient.bean.CustomerEntity;
import cn.jksoft.businessclient.ui.adapter.CustomerSelectAdapter;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RogersHuang on 16/4/8.
 */
public class CustomerSelectActivity extends BaseSwipeBackCompatActivity implements View.OnClickListener {
    @Bind(R.id.listView)
    IndexableStickyListView mListView;
    @Bind(R.id.btn_add_car_num)
    Button btnAddCarNum;
    private CustomerEntity customerEntity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_select);
        ButterKnife.bind(this);
        initTitle("客户选择");
        initBackClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getCustomerListData();
    }

    private void getCustomerListData(){
        JSONObject json = null;
        try {
            json = new JSONObject();
            json.put("storeId", BaseApplication.getInstance().getLoginEntity().getPuxsoftLoginDTO().getStoreId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        ReceivingService.createReceivingService().getCustomerList(json.toString())
                .observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CustomerEntity>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(CustomerEntity entity) {
                        if("1".equals(entity.getIs_successful())) {
                            customerEntity = entity;
                            operationCustomerData();
                        }
                    }
                });
    }

    private void operationCustomerData(){
        List<CustomerEntity.StoreCustomerListBean> datas = new ArrayList<CustomerEntity.StoreCustomerListBean>();
        for (CustomerEntity.StoreCustomerListBean bean :customerEntity.getStoreCustomerList()){
            bean.setName(bean.getCUSTOMER_NAME());
            datas.add(bean);
        }
        setupListView(datas);
    }

    private void setupListView(List<CustomerEntity.StoreCustomerListBean> datas){
        CustomerSelectAdapter adapter = new CustomerSelectAdapter(this);
        mListView.setAdapter(adapter);
        mListView.bindDatas(datas);
        mListView.setOnItemContentClickListener(new IndexableStickyListView.OnItemContentClickListener() {
            @Override
            public void onItemClick(View v, IndexEntity indexEntity) {
                CustomerEntity.StoreCustomerListBean contactEntity = (CustomerEntity.StoreCustomerListBean) indexEntity;
                Intent data = new Intent();
                data.putExtra("customerId", contactEntity.getCUSTOMER_ID());
                data.putExtra("custCmId", contactEntity.getCUST_CM_ID());
                data.putExtra("customerName", contactEntity.getCUSTOMER_NAME());
                data.putExtra("carPlateNum",contactEntity.getCAR_PLATE_NUM());
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }

    @Override
    @OnClick({R.id.btn_add_car_num})
    public void onClick(View v) {
        readyGoForResult(ReceivingCarActivity.class,0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            CustomerEntity.StoreCustomerListBean bean  =  (CustomerEntity.StoreCustomerListBean)data.getSerializableExtra("customer");
            Intent intent = new Intent();
            intent.putExtra("customerId", bean.getCUSTOMER_ID());
            intent.putExtra("custCmId", bean.getCUST_CM_ID());
            intent.putExtra("customerName", bean.getCUSTOMER_NAME());
            intent.putExtra("carPlateNum",bean.getCAR_PLATE_NUM());
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
